package com.crm.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.QueryDictData;
import com.crm.domain.entity.DictData;
import com.crm.service.system.DictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Xanny
 */
@RestController
@RequestMapping("/system/dict/data")
@RequiredArgsConstructor
public class DictDataController {
    private final DictDataService dictDataService;

    /**
     * 完成dict_type对应data的查询
     * @param dictType
     * @return
     */
    @GetMapping("/type/{dictType}")
    public ResponseResult getByDictType(@PathVariable String dictType) {
        List<DictData> list = dictDataService.lambdaQuery().eq(DictData::getDictType,dictType).list();
        return ResponseResult.success(list);
    }


    /**
     * 完成字典dictType的查询
     * @return
     */
    @GetMapping("/list")
    public ResponseResult searchList(QueryDictData queryDictData){
        Page<DictData> dictTypeList = dictDataService.queryAll(queryDictData);
        return ResponseResult.success()
                .put("rows", dictTypeList.getRecords())
                .put("total", dictTypeList.getTotal());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseResult getById(@PathVariable Integer id) {
        DictData dictData = dictDataService.lambdaQuery().eq(DictData::getDictCode, id).one();
        return ResponseResult.success(dictData);
    }
    /**
     * 新增字典数据
     * @param dictData
     * @return
     */
    @PostMapping
    public ResponseResult saveDictData(@RequestBody DictData dictData){
        dictDataService.save(dictData);
        return ResponseResult.success();
    }

    /**
     * 修改字典数据
     * @param dictData
     * @return
     */
    @PutMapping
    public ResponseResult updateDictData(@RequestBody DictData dictData){
        dictDataService.lambdaUpdate().eq(DictData::getDictCode,dictData.getDictCode()).update(dictData);
        return ResponseResult.success();
    }

    /**
     * 删除字典数据
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseResult deleteDictData(@PathVariable Integer id){
        dictDataService.lambdaUpdate().eq(DictData::getDictCode,id).remove();
        return ResponseResult.success();
    }
}
